package com.sixfoot.domain;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.sixfoot.foundation.persistence.SetEntity;

/**
 * 部门角色，用来记录某个部门某个角色所拥有的权限
 * 
 * @author six-foot
 *
 */
@Entity
@Table(name = "organization_role_permissions")
public class OrganizationRolePermissions extends SetEntity<Permission> {
	/** 关联角色 */
	private Role role;
	/** 关联部门 */
	private Organization organization;
	/** 部门角色特殊权限 */
	private Set<Permission> permissions;
	
	@ManyToOne
	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	@ManyToOne
	public Organization getOrganization() {
		return organization;
	}

	public void setOrganization(Organization organization) {
		this.organization = organization;
	}

	@ManyToMany
	public Set<Permission> getPermissions() {
		return permissions;
	}

	public void setPermissions(Set<Permission> permissions) {
		this.permissions = permissions;
	}

	@Override
	@Transient
	protected Set<Permission> getInternalSet() {
		if (permissions == null)
			permissions = new HashSet<>();
		return permissions;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null)
			return false;
		if (getId() == null)
			return super.equals(obj);
		if (obj instanceof OrganizationRolePermissions)
			return false;
		OrganizationRolePermissions drole = (OrganizationRolePermissions) obj;
		return (getId().equals(drole.getId()) || (getOrganization().getId()
				.equals(drole.getOrganization().getId()) && getRole().getId()
				.equals(drole.getRole().getId())));
	}
}
